package router

import (
	"software/controller"
	"software/middleware"

	"github.com/gin-gonic/gin"
)

func SetupRouter() *gin.Engine {
	r := gin.Default()

	// CORS中间件
	r.Use(func(c *gin.Context) {
		c.Header("Access-Control-Allow-Origin", "*")
		c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
		c.Header("Access-Control-Allow-Headers", "Origin, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")

		if c.Request.Method == "OPTIONS" {
			c.AbortWithStatus(204)
			return
		}

		c.Next()
	})

	// API路由组
	api := r.Group("/api")
	{
		// 认证相关路由
		auth := api.Group("/auth")
		{
			auth.POST("/check-email", controller.CheckEmail)
			auth.POST("/send-code", controller.SendVerificationCode)
			auth.POST("/login", controller.Login)
			auth.POST("/login-with-code", controller.LoginWithCode)
			auth.POST("/register", controller.Register)
			auth.POST("/update-password", controller.UpdatePassword)
		}

		// 需要登录鉴权的业务API示例
		protected := api.Group("/protected")
		protected.Use(middleware.JWTAuthMiddleware())
		{
			protected.GET("/me", controller.GetCurrentUser)
			protected.POST("/update", controller.UpdateCurrentUser)
			protected.POST("/upload-avatar", controller.UploadAvatar)
		}

		// 简历相关api
		resume := protected.Group("/resume")
		{
			resume.POST("/upload", controller.UploadResume)
			resume.GET("/list", controller.GetResumeList)
			resume.DELETE("/:id", controller.DeleteResume)
			resume.POST("/set-default/:id", controller.SetDefaultResume)
		}

		// 面试场景相关api
		scene := protected.Group("/interview_scene")
		{
			scene.GET("/", controller.GetInterviewScenes)
			scene.GET(":id", controller.GetInterviewScene)
			scene.POST("/", controller.CreateInterviewScene)
			scene.PUT(":id", controller.UpdateInterviewScene)
			scene.DELETE(":id", controller.DeleteInterviewScene)
		}
	}

	r.Static("/uploads", "uploads") // 静态资源映射

	return r
}
